<template>
  <div id="app" style="width: 100%">
    <el-card class="box-card">
      <div class="select-contents">
        <!-- 添加管理公司 -->
        <el-button type="primary" @click="clickCompanyShow"
          >添加管理公司</el-button
        >
        <!-- 筛选管理公司 -->
        <div class="select-right">
          <el-input
            style="margin-left: 20px"
            v-model="deptName"
            placeholder="请输入管理公司"
          ></el-input>
          <el-button
            style="margin-left: 20px"
            type="primary"
            @click="clickSelectCompany"
            >查询</el-button
          >
        </div>
      </div>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="box-card">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="deptName" label="管理公司" />
        <el-table-column prop="companyAddress" label="管理公司地址" />
        <el-table-column prop="contact" label="管理公司联系人" />
        <el-table-column prop="contactPhone" label="联系人电话" />
        <el-table-column prop="contactAddress" label="联系人地址" />
        <el-table-column prop="contactFax" label="联系人传真" />
        <el-table-column prop="contactEmail" label="联系人邮箱" />
        <el-table-column prop="contactRemark" label="联系人备注" />
        <el-table-column fixed="right" label="操作" width="140">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button
              type="text"
              @click="clickEditingShip(scope.row)"
              size="small"
              >编辑</el-button
            >
            <el-popconfirm
              title="确认删除此公司吗？"
              @confirm="clickDeleteShip(scope.row)"
            >
              <el-button
                type="text"
                style="margin-left: 10px"
                size="small"
                slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-card class="box-card" style="text-align: right" v-if="totalNum > 10">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="currentPage"
        :page-size="pagination.count"
        :total="totalNum"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>

    <!-- 添加公司 -->
    <el-dialog
      :title="!editing ? '添加公司信息' : '修改公司信息'"
      :visible.sync="companyShow"
      width="50%"
    >
      <el-form :model="shipModelMessage" :rules="rules" label-width="130px">
        <el-form-item label="管理公司" class="form-box" prop="company">
          <el-input
            v-model="shipModelMessage.company"
            placeholder="请输入管理公司"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="管理公司地址"
          class="form-box"
          prop="companyAddress"
        >
          <el-input
            v-model="shipModelMessage.companyAddress"
            placeholder="请输入管理公司地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="管理公司联系人" class="form-box" prop="contact">
          <el-input
            v-model="shipModelMessage.contact"
            placeholder="请输入管理公司联系人"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人电话" class="form-box" prop="contactPhone">
          <el-input
            type="number"
            v-model="shipModelMessage.contactPhone"
            placeholder="请输入联系人电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人地址" class="form-box">
          <el-input
            v-model="shipModelMessage.contactAddress"
            placeholder="请输入联系人地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人传真" class="form-box">
          <el-input
            v-model="shipModelMessage.contactFax"
            placeholder="请输入联系人传真"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人邮箱" class="form-box">
          <el-input
            v-model="shipModelMessage.contactEmail"
            placeholder="请输入联系人邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="创建用户" v-if="!editing" class="form-box">
          <div class="switch-content">
            <el-switch v-model="shipModelMessage.delivery" disabled></el-switch>
            <div class="tips">
              注：创建管理公司将同步创建用户，用户名将随机生成，密码默认为<span
                style="color: #000"
                >Hx789564</span
              >
            </div>
          </div>
        </el-form-item>
        <el-form-item label="联系人备注" class="form-box">
          <el-input
            type="textarea"
            v-model="shipModelMessage.contactRemark"
            placeholder="请输入联系人备注"
          ></el-input>
        </el-form-item>

        <div class="shipBottom">
          <el-button @click="clickReset" type="info" plain>重置</el-button>
          <el-button @click="submitForm" type="primary">提交</el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 查看公司 -->
    <el-dialog title="公司信息" :visible.sync="companyMessageShow" width="50%">
      <div class="popup-content">
        <div class="popup-title">公司信息</div>
        <div class="popup-box">
          <div class="popup-item">
            <span style="color: #000">管理公司：</span
            >{{ companyMessage.company || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">管理公司地址：</span
            >{{ companyMessage.companyAddress || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">管理公司联系人：</span
            >{{ companyMessage.contact || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人电话：</span
            >{{ companyMessage.contactPhone || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人地址：</span
            >{{ companyMessage.contactAddress || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人传真：</span
            >{{ companyMessage.contactFax || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人邮箱：</span
            >{{ companyMessage.contactEmail || "--" }}
          </div>
          <div class="popup-item">
            <span style="color: #000">联系人备注：</span
            >{{ companyMessage.contactRemark || "--" }}
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import uiHeader from "../layout/UiHeader.vue";

export default {
  name: "company",
  components: {
    uiHeader,
  },
  data() {
    return {
      deptName: "",
      companyShow: false,
      editing: false,
      companyMessageShow: false,
      shipModelMessage: {
        id: "",
        delivery: true,
        company: "",
        companyAddress: "",
        contact: "",
        contactPhone: "",
        contactAddress: "",
        contactFax: "",
        contactEmail: "",
        contactRemark: "",
      },
      rules: {
        company: [
          { required: true, message: "请输入管理公司", trigger: "blur" },
        ],
        companyAddress: [
          { required: true, message: "请输入管理公司地址", trigger: "blur" },
        ],
        contact: [
          { required: true, message: "请输入管理公司联系人", trigger: "blur" },
        ],
        contactPhone: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
        ],
      },
      totalNum: 0,
      companyMessage: {},
      tableData: [],
      pagination: {
        page: 1,
        count: 10,
      },
    };
  },
  mounted() {
    this.getCompanyShips();
  },
  methods: {
    // 搜索
    clickSelectCompany() {
      this.getCompanyShips();
    },
    // 重置参数
    clickReset() {
      this.shipModelMessage = {
        delivery: true,
        company: "",
        companyAddress: "",
        contact: "",
        contactPhone: "",
        contactAddress: "",
        contactFax: "",
        contactEmail: "",
        contactRemark: "",
      };
    },
    clickCompanyShow() {
      this.clickReset();
      this.companyShow = true;
      this.editing = false;
    },
    submitForm(formName) {
      if (this.shipModelMessage.company == "") {
        this.$message.error("请输入管理公司");
      } else if (this.shipModelMessage.companyAddress == "") {
        this.$message.error("请输入管理公司地址");
      } else if (this.shipModelMessage.contact == "") {
        this.$message.error("请输入管理公司联系人");
      } else if (this.shipModelMessage.contactPhone == "") {
        this.$message.error("请输入联系人电话");
      } else {
        this.clickAddCompany();
      }
    },
    // 添加公司
    clickAddCompany() {
      const that = this;
      that.shipModelMessage.deptName = that.shipModelMessage.company;
      // 0为公司 1为船舶, 2为船队
      that.shipModelMessage.kind = "0";
      delete this.shipModelMessage.messageList;
      let url = that.editing
        ? `api/department/update-by-id`
        : `/api/department/add`;
      const formData = new FormData();
      for (const key in this.shipModelMessage) {
        if (key != "children") {
          formData.append(key, this.shipModelMessage[key]);
        }
      }
      that
        .$axios({
          method: "post",
          url: url,
          data: formData,
          headers: {
            "Content-Type": "multipart/form-data", // 设置正确的 Content-Type
          },
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.$message.success("操作成功！");
            this.clickReset();
            this.getCompanyShips();
            this.companyShow = false;
            this.editing = false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 编辑公司
    clickEditingShip(item) {
      this.shipModelMessage = item;
      this.companyShow = true;
      this.editing = true;
    },
    // 获取公司列表
    getCompanyShips() {
      const that = this;
      that
        .$axios({
          method: "get",
          url: `/api/department/dept/ships`,
          params: {
            page: that.pagination.page,
            count: that.pagination.count,
            deptName: that.deptName,
            kinds: 0,
            parentId: 0,
          },
        })
        .then((res) => {
          if (res.data.code == 0) {
            that.totalNum = res.data.data.total;
            that.tableData = res.data.data.list;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.pagination.page = val;
      this.getCompanyShips();
    },
    // 查看公司
    handleClick(item) {
      this.companyMessage = item;
      this.companyMessageShow = true;
    },
    // 删除公司
    clickDeleteShip(item) {
      const that = this;
      that
        .$axios({
          method: "get",
          url: `/api/department/del-by-id?id=` + item.id,
        })
        .then((res) => {
          if (res.data.code == 0) {
            that.$message.success("操作成功！");
            that.getCompanyShips();
          } else {
            that.$message.warning(res.data.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
  <style>
.box-card {
  margin-top: 20px;
}
.select-contents {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.select-right {
  display: flex;
  align-items: center;
}
.switch-content {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
}
.tips {
  width: 100%;
  text-align: left;
  font-size: 12px;
  color: #666;
}
.popup-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  padding: 0 20px;
}
.popup-content .popup-item {
  width: 45%;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  font-size: 14px;
  color: #666;
}
.popup-content .popup-item span {
  font-size: 15px;
  font-weight: bold;
  width: 120px;
  text-align: right;
}
.popup-content .popup-title {
  /* width: 100%; */
  text-align: left;
  margin: 0 0 10px 0;
  padding: 0 10px;
  margin-left: 12px;
  background-color: #fff;
  position: relative;
  font-size: 16px;
}
.popup-box {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  padding: 20px 20px 0 20px;
  border-radius: 12px;
  margin: -20px 0 10px;
  border: 1px solid #999;
}
</style>
  